<?php

namespace App\Models;

use App\Util\Tool;
use Illuminate\Database\Eloquent\Model;

class Directory extends Model
{
    protected $table = 'c_courses_section';

    /**
     * 获得此评论所属的文章。
     */
    public function course()
    {
        return $this->belongsTo('App\Models\Course',"courses_id","id");
    }

    /****
     * @param $filter
     * @return mixed
     * 章节详情
     */
    public function getChapterInfo($filter)
    {
//        $work_course = (new WorkRelateCourse());
        $course = new Course();
        $tool = new Tool();
//        $all_id = $work_course::getIdsByRelateId($filter['wc_relate_id']);
        $chapter = $this->where("id", '=', $filter['chapter_id'])->with(['course'])->first();
        if (isset($filter['user_id']) && isset($filter['work_id'])) {
            $filter['course_id'] = $chapter->courses_id;
            $chapter->is_pay = $course->getIsPay($filter, $filter['user_id']);
        } else {
            $chapter->is_pay = 0;
        }
//        $chapter->chapter_img = $tool->addHostToImage($chapter->chapter_img);
        $chapter->courses_video = $tool->addHostToImage($chapter->courses_video);
//        $chapter->wc_relate_id = $filter['wc_relate_id'];
        if (!empty($chapter)) {
            $chapter = $chapter->toArray();
        }
        return $chapter;
    }

    /****
     * @param $filter
     * @return mixed
     * 章节详情内容
     */
    public function getChapterInfoByRelateId($filter)
    {

        return $this->where("id", '=', $filter['chapter_id'])->with(['course'])->first();
    }
}
